package com.ymatou.shop.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.ymatou.shop.Constants;
import com.ymatou.shop.account.Account;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.ResMethod;
import com.ymatou.shop.cache.ResType;
import com.ymatou.shop.util.DateUtil;
import com.ymatou.shop.util.GlobalUtil;

public final class ApiRequest {

	private ApiRequest() {
	}

	/**
	 * 登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public static RequestInfo getLoginRequest(String username, String password) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("Username", username));
		params.add(new BasicNameValuePair("Password", password));
		//		params.add(new BasicNameValuePair("Password", MD5.md5Hex(password)));

		return new RequestInfo(ResType.api, Constants.URL_LOGIN).setExpires(RequestInfo.EXPIRES_NO_CACHE).setUseCache(false).setMethod(ResMethod.post).setParams(params);
	}

	/**
	 * 验证买家邀请码
	 * 
	 * @param invitationCode
	 * @param context
	 * @return
	 */
	public static RequestInfo invitationCode(String invitationCode) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("InvitationCode", invitationCode));
		return new RequestInfo(ResType.api, Constants.URL_GET_INVITATIONCODE).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setExpires(RequestInfo.EXPIRES_NO_CACHE).setUseCache(false).setParams(params);
	}

	/**
	 * 获取用户权限
	 * 
	 * @param context
	 * @return
	 */
	public static RequestInfo permission() {
		return new RequestInfo(ResType.api, Constants.URL_GET_PERMISSION).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setExpires(RequestInfo.EXPIRES_NO_CACHE).setUseCache(false);
	}
	
	/**
	 * 取消授权
	 * @param context
	 * @return
	 */
	public static RequestInfo cancelPermission() {
		return new RequestInfo(ResType.api, Constants.URL_CANCEL_PERMISSION).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setExpires(RequestInfo.EXPIRES_NO_CACHE).setUseCache(false);
	}

	/*
	 * 
	 */
	public static RequestInfo getSellerInfo(int sellerId) {
		JSONObject params = new JSONObject();
		try {
			params.put("SellerId", sellerId);
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}

		return new RequestInfo(ResType.api, Constants.URL_GET_SELLER_INFO).setMethod(ResMethod.get).setParams(params);
	}

	/**
	 * 获取用户信息 判断卖家or买家
	 * 
	 * @param accounts
	 * @return
	 */
	public static RequestInfo getUserinfo() {
		return new RequestInfo(ResType.api, Constants.URL_GET_USER_INFO)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
				.setMethod(ResMethod.post)
				.setExpires(RequestInfo.EXPIRES_NO_CACHE)
				.setUseCache(false);
	}
	
	/**
	 * 获取可支持银行
	 * @return
	 */
	public static RequestInfo getPayBankList() {
		return new RequestInfo(ResType.api, Constants.URL_GET_PAY_BANKLIST)
//		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setMethod(ResMethod.get);
//		.setExpires(RequestInfo.EXPIRES_NO_CACHE)
	}
	
	/**
	 * 获取用户收货地址列表
	 * @return
	 */
	public static RequestInfo getUserAddress() {
		return new RequestInfo(ResType.api, Constants.URL_GET_USER_ADDRESS)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setMethod(ResMethod.get)
		.setExpires(RequestInfo.EXPIRES_NO_CACHE)
		.setUseCache(false);
	}
	
	/**
	 * 增加用户收货地址
	 * @param addressName
	 * @param area
	 * @param detailAddress
	 * @param phone
	 * @param email
	 * @return
	 */
	public static RequestInfo addUserAddress(String addressName, String area, String detailAddress, String phone, String email) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("Addressee", addressName));
		params.add(new BasicNameValuePair("Area", area));
		params.add(new BasicNameValuePair("DetailAddress", detailAddress));
		params.add(new BasicNameValuePair("Phone", phone));
		params.add(new BasicNameValuePair("Email", email));
		return new RequestInfo(ResType.api, Constants.URL_ADD_USER_ADDRESS)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setParams(params)
		.setMethod(ResMethod.post);
	}
	
	
	/**
	 * 保存订单并支付
	 * @param userId
	 * @param productId
	 * @param productNum 目前默认为1
	 * @param addressId
	 * @param leaveWord
	 * @param useAccountAmount 使用码头余额数量, 不使用则为0
	 * @param useGiftAmount
	 * @param tradingPasswrod 支付密码（不使用余额，可为空）
	 * @param payBank 支付银行的代码(只使用余额支付，可为空)
	 * @return
	 
	public static RequestInfo saveOrder(String userId, String productId, String productNum, String addressId, String leaveword
			,String useAccountAmount, String useGiftAmount, String tradingPasswrod, String payBank) {
		JSONObject mParams = new JSONObject();
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("UserId", userId));
		params.add(new BasicNameValuePair("ProductId", productId));
		params.add(new BasicNameValuePair("ProductNum", productNum));
		params.add(new BasicNameValuePair("AddressId", addressId));
		params.add(new BasicNameValuePair("LeaveWord", leaveword));
		
		List<NameValuePair> params2 = new ArrayList<NameValuePair>();
		params2.add(new BasicNameValuePair("UseAccountAmount", useAccountAmount));
		params2.add(new BasicNameValuePair("UseGiftAmount", useGiftAmount));
		params2.add(new BasicNameValuePair("TradingPasswrod", tradingPasswrod));
		params2.add(new BasicNameValuePair("PayBank", payBank));
		
		JSONObject mParams2 = new JSONObject();
		JSONObject mParams3 = new JSONObject();
		try {
	    mParams2.put("UserId", userId);
	    mParams2.put("ProductId", productId);
			mParams2.put("ProductNum", productNum);
			mParams2.put("AddressId", addressId);
			mParams2.put("LeaveWord", leaveword);
			mParams3.put("UseAccountAmount", useAccountAmount);
			mParams3.put("UseGiftAmount", useGiftAmount);
			mParams3.put("TradingPasswrod", tradingPasswrod);
			mParams3.put("PayBank", payBank);
    } catch (JSONException e1) {
	    e1.printStackTrace();
    }
		try {
	    mParams.put("OrderInfo", mParams2);
	    mParams.put("PaymentInfo", mParams3);
    } catch (JSONException e) {
	    e.printStackTrace();
    }
		return new RequestInfo(ResType.api, Constants.URL_SAVE_ORDER)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setParams(mParams)
		.setMethod(ResMethod.post);
	}
	*/
	
	/**
	 * 保存订单
	 * @param userId 
	 * @param productId 商品id
	 * @param productNum 商品num
	 * @param addressId 商品地址
	 * @param leaveword 留言
	 * @param useGiftAmount 红包
	 * @return
	 */
	public static RequestInfo saveOrder(String productId, String productNum, String addressId, String leaveword
			,String useGiftAmount) {
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		params.add(new BasicNameValuePair("ProductNum", productNum));
		params.add(new BasicNameValuePair("AddressId", addressId));
		params.add(new BasicNameValuePair("LeaveWord", leaveword));
		params.add(new BasicNameValuePair("UseGiftAmount", useGiftAmount));
		return new RequestInfo(ResType.api, Constants.URL_SAVE_ORDER)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setParams(params)
		.setMethod(ResMethod.post);
	}
	
	/**
	 * 支付订单
	 * @param orderId
	 * @param userAccount
	 * @param tradingPasswrod
	 * @return
	 */
	public static RequestInfo payOrder(String orderId, Boolean userAccount, String tradingPasswrod) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("OrderId", orderId));
		params.add(new BasicNameValuePair("UseAccount", userAccount.toString()));
		params.add(new BasicNameValuePair("TradingPasswrod", tradingPasswrod));
		return new RequestInfo(ResType.api, Constants.URL_SAVE_ORDER)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setParams(params)
		.setMethod(ResMethod.post);
	}
	
	/**
	 * 删除用户收货地址
	 * @param AddressId
	 * @return
	 */
	public static RequestInfo deleteUserAddress(String AddressId) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("AddressId", AddressId));
		return new RequestInfo(ResType.api, Constants.URL_DELETE_USER_ADDRESS)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setParams(params)
		.setMethod(ResMethod.post);
	}
	
	/**
	 * 发评论
	 * @param productId
	 * @param content
	 * @return
	 */
	public static RequestInfo sendComment(String productId, String content) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		params.add(new BasicNameValuePair("Content", content));
		return new RequestInfo(ResType.api, Constants.URL_SEND_COMMENT)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setMethod(ResMethod.post)
		.setExpires(RequestInfo.EXPIRES_NO_CACHE)
		.setParams(params)
		.setUseCache(false);
	}

	/**
	 * 回复评论
	 * @param commentId
	 * @param content
	 * @return
	 */
	public static RequestInfo sendCommentReply(String commentId, String content) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("CommentId", commentId));
		params.add(new BasicNameValuePair("Content", content));
		return new RequestInfo(ResType.api, Constants.URL_SEND_COMMENT_REPLY)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setMethod(ResMethod.post)
		.setExpires(RequestInfo.EXPIRES_NO_CACHE)
		.setParams(params)
		.setUseCache(false);
	}

	/**
	 * 获取用户账号信息
	 * @return
	 */
	public static RequestInfo getAccountInfo() {
		return new RequestInfo(ResType.api, Constants.URL_GET_ACCOUNT_INFO)
		.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		.setMethod(ResMethod.get)
		.setExpires(RequestInfo.EXPIRES_NO_CACHE)
		.setUseCache(false);
	}

	/**
	 * 
	 * 添加商品 //用户Id UserId : xxxxx, //商品发布的地理位置，最好返回对应城市 UserPosition : “xxxxxxxxx”
	 * //商品图片, 第一个为主图, 最少一个上传的图片地址 ProductPics : [“http://xxxxxx”,
	 * “http://xxxxxxx”] , //商品描述， 最多60字，必填 ProductDes : “商品描述”, //库存, 整数， 必填
	 * StockNum: xx, //预估价，整数, 必填 Price: xxxxx, //商品有效期, 时间格式 yyyy-MM-dd HH:mm:ss，
	 * 必填 ExpireTime : “xxxx-xx-xx xx:xx:xx” //关税担负方，枚举值 0为卖家，1为买家 TariffType: x
	 * 
	 * @param userPosition
	 * @param productPics
	 * @param productDes
	 * @param stockNum
	 * @param price
	 * @param expireTime
	 * @param tariffType
	 * @return
	 */
	public static RequestInfo addProduct(String userPosition, String productPics, String productDes, String stockNum, String price, String expireTime, int tariffType) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userPosition", userPosition));
		params.add(new BasicNameValuePair("productPics", productPics.toString()));
		params.add(new BasicNameValuePair("productDes", productDes));
		params.add(new BasicNameValuePair("stockNum", stockNum));
		params.add(new BasicNameValuePair("productDes", productDes));
		params.add(new BasicNameValuePair("price", price));
		params.add(new BasicNameValuePair("expireTime", expireTime));
		params.add(new BasicNameValuePair("tariffType", String.valueOf(tariffType)));

		return new RequestInfo(ResType.api, Constants.URL_ADD_PRODUCT).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setParams(params);
	}

	/**
	 * 修改库存 UserId :xxx, //用户Id ProductId: “xxxxx”, //商品Id StockNum: xxxx, //库存,
	 * 范围0~99 AccessToken:xxxx //用户令牌
	 * 
	 * @param userId
	 * @param token
	 * @param productId
	 * @param stockNum
	 * @return
	 */
	public static RequestInfo changeStock(String productId, int stockNum) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		params.add(new BasicNameValuePair("stockNum", String.valueOf(stockNum)));

		return new RequestInfo(ResType.api, Constants.URL_ADD_PRODUCT).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setParams(params);
	}

	/**
	 * 开始新活动  Position:xxx //用户所在地理位置 
	 * 
	 * @param position
	 * @return
	 */
	public static RequestInfo createActivity(String position) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("position", position));
		return new RequestInfo(ResType.api, Constants.URL_CREATE_ACTIVITY).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setParams(params);
	}
	
	/**
	 * 删除商品
	 * @param productId
	 * @return
	 */
	public static RequestInfo deleteProduct(String productId) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		return new RequestInfo(ResType.api, Constants.URL_DELETE_PRODUCT).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setParams(params);
	}
	
	/**
	 * 商品下架
	 * @param productId
	 * @return
	 */
	public static RequestInfo setOffProduct(String productId) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		return new RequestInfo(ResType.api, Constants.URL_SET_OFF_PRODUCT).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setParams(params);
	}
	
	/**
	 * 重新上架
	 * @param productId
	 * @return
	 */
	public static RequestInfo setOnProduct(String productId) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		return new RequestInfo(ResType.api, Constants.URL_SET_ON_PRODUCT).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post).setParams(params);
	}
	
	/**
	 * 结束活动
	 * @return
	 */
	public static RequestInfo closeActivity(String position) {
		return new RequestInfo(ResType.api, Constants.URL_CLOSE_ACTIVITY).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.post);
	}

	/**
	 * 
	 * @param accounts
	 * @return
	 */
	public static RequestInfo getSellerProductList() {
		return new RequestInfo(ResType.api, Constants.URL_GET_PRODUCT_LIST).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setMethod(ResMethod.get);
	}

	/**
	 * 请求消息数
	 * 
	 * @param accounts
	 * @return
	 */
	public static RequestInfo getNewCommentNum() {

		JSONObject params = new JSONObject();
		try {
			params.put("StatisticTime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}

		return new RequestInfo(ResType.api, Constants.URL_GET_NEW_COMMENT_NUM)
					.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		    .setMethod(ResMethod.get).setParams(params);
	}
	
	/**
	 * 
	 * @param productId 相关商品Id
	 * @param commentNum 获取评论数量
	 * @param lastCommentId 最后一个评论Id 可选
	 * @return
	 */
	public static RequestInfo getCommentList(String productId, String commentNum, String lastCommentId) {
		
		JSONObject params = new JSONObject();
		try {
			params.put("ProductId", productId);
			params.put("CommentNum", commentNum);
			params.put("LastCommentId", lastCommentId);
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}
		
		return new RequestInfo(ResType.api, Constants.URL_GET_PRODUCT_COMMENT)
				.setMethod(ResMethod.get)
				.setParams(params);
	}

	/**
	 * 请求订单数
	 * 
	 * @param accounts
	 * @return
	 */
	public static RequestInfo getNewOrderNum() {

		JSONObject params = new JSONObject();
		try {
			params.put("StatisticTime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}

		return new RequestInfo(ResType.api, Constants.URL_GET_NEW_ORDER_NUM)
					.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
		    .setMethod(ResMethod.get).setParams(params);
	}

	/**
	 * 获取首页产品
	 * 
	 * @return
	 */
	public static RequestInfo getHomeList() {
		return new RequestInfo(ResType.api, Constants.URL_GET_HOME_PRODUCT_LIST).setMethod(ResMethod.get);
	}

	public static RequestInfo getSellerNewProductNum(int sellerId, long time) {
		JSONObject params = new JSONObject();
		try {
			params.put("SellerId", sellerId);
			params.put("StatisticTime", DateUtil.format(time, DateUtil.ISO_DATETIME_FORMAT_SORT));
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}

		return new RequestInfo(ResType.api, Constants.URL_GET_SELLER_NEW_PRODUCT_NUM).setMethod(ResMethod.get).setParams(params);
	}
	
	public static RequestInfo getSellerEventDetail(String productId, boolean useCache) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ProductId", productId));
		
		return new RequestInfo(ResType.api, Constants.URL_GET_SELLER_EVENT_PRODUCT)
					.setUseCache(useCache);
	}

	public static RequestInfo getSellerEventProductList(int activityId, String lastProductId, int pageSize, boolean useCache) {
		return new RequestInfo(ResType.api, "http://app.alpha.ymatou.com/api/activity/ListProducts?ActivityId=10&ProductNum=252010");
//		JSONObject params = new JSONObject();
//		try {
//			params.put("ActivityId", activityId);
//			params.put("LastProductId", lastProductId);
//			if (pageSize > 0) {
//				params.put("ProductNum", pageSize);
//			}
//		} catch (JSONException e) {
//			GlobalUtil.logE(e.toString(), e);
//		}
//		List<NameValuePair> params = new ArrayList<NameValuePair>();
//		params.add(new BasicNameValuePair("ActivityId", String.valueOf(activityId)));
//		params.add(new BasicNameValuePair("LastProductId", lastProductId));
//		if (pageSize > 0) {
//			params.add(new BasicNameValuePair("ProductNum", String.valueOf(pageSize)));
//		}
//		
//		return new RequestInfo(ResType.api, Constants.URL_GET_SELLER_EVENT_PRODUCT_LIST)
//					.setMethod(ResMethod.get)
//					.setParams(params);
	}

	public static RequestInfo getSellerProductList(boolean useCache) {
		return new RequestInfo(ResType.api, Constants.URL_GET_BUYER_ORDER_LIST).setMethod(ResMethod.get).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setExpires(RequestInfo.EXPIRES_NO_CACHE).setUseCache(false);
	}

	public static RequestInfo getSellerProductLeaveMessageList(String productId, int sellerId, boolean useCache) {

		JSONObject params = new JSONObject();
		try {
			params.put("ProductId", productId);
			params.put("UserId", sellerId);
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}

		return new RequestInfo(ResType.api, Constants.URL_GET_SELLER_PRODUCT_LEAVE_MESSAGE_LIST).setMethod(ResMethod.get).setAuthLevel(RequestInfo.AUTHLEVEL_USER).setExpires(RequestInfo.EXPIRES_NO_CACHE).setUseCache(false);
	}
	
	public static RequestInfo getBuyerOrderList(String lastId) {
		
		JSONObject params = new JSONObject();
		try {
			params.put("LastOrderId", lastId);
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}
		
		return new RequestInfo(ResType.api, Constants.URL_GET_SELLER_ORDER_LIST)
			.setMethod(ResMethod.get)
			.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
			.setExpires(RequestInfo.EXPIRES_NO_CACHE)
			.setUseCache(false)
			.setParams(params);
	}
	
	public static RequestInfo getMessageListRequest(int type, int pageSize,
			String lastId) {
		JSONObject params = new JSONObject();
		try {
			params.put("CommentNum", pageSize);
			params.put("LastCommentId", lastId);
		} catch (JSONException e) {
			GlobalUtil.logE(e.toString(), e);
		}
		
		String requestUrl = null;
		if (type == Constants.TYPE_MESSAGE_BUYER_MESSAGE) {
			requestUrl = Constants.URL_MESSAGE_BUYER_LIST;
		} else {
			requestUrl = Constants.URL_MESSAGE_SELLER_LIST;
		}
		
		return new RequestInfo(ResType.api, requestUrl)
				.setAuthLevel(RequestInfo.AUTHLEVEL_USER)
				.setExpires(RequestInfo.EXPIRES_NO_CACHE)
				.setUseCache(false)
				.setParams(params);
	}

	public static RequestInfo getImageRequest(String url) {
		return new RequestInfo(ResType.image, url);
	}
}
